# -*- coding: utf-8 -*-

import shutil
import glob
import os


class Bunch(object):
    def __init__(self, **kwds):
        self.__dict__.update(kwds)
        
        
def subdict(d, ks):
    """ 
    >>> d = dict(x=1, y=2, z=3, t=1000)
    >>> ks = ('x', 't', 'not_exist')
    >>> d_proj = subdict(d, ks)
    >>> {'x':1, 't':1000}
    """
    res = []
    for _k in ks:
        try:
            res.append((_k, d[_k]))
        except KeyError:
            pass
    return dict(res)


def project2dict(obj, domain):
    """
    >>> a.x, a.y, a.z
    >>> 1, 2, 3
    >>> a_proj = project2dict(a, ('x', 'y', 't'))
    >>> {'x':1, 'y':2}
    """
    if isinstance(obj, dict):
        return subdict(obj, domain)
        
    res = []
    for _k in domain:
        try:
            res.append( (_k, obj.__getattribute__(_k)) )
        except AttributeError:
            pass
    return dict(res)


def copy_files(src_dir, dst_dir, pattern, exc=None):
    """
    Copies files from src_dir to dst_dir.
    !!!***SIDE EFFECT***!!!
        :pattern: filter files by name
        :exc: file exceptions set. This files are not copied.
    """
    join = os.path.join
    
    fn_list = [_fn[_fn.rfind('/') + 1:] for _fn in glob.glob(join(src_dir, pattern))]
    
    for fn in (_fn for _fn in fn_list if _fn not in exc):
        shutil.copy2(join(src_dir, fn), join(wrk_dir, fn))